// pages/user/user.js
import http from '../../http'

Page({
  data: {
    defaultAvatarUrl: '',
    userInfo: {},
    // 我的订单
    orderList: [],
    userInfo: {
      avatarUrl: '',
      nickName: '正在登录...',
      phoneNumber: '',
    },
    // 登录状态
    currAuthStep: 1,
  },
  // 订单
  orderTap(e) {
    const status = e.currentTarget.dataset.status;
    console.log(status);
    wx.navigateTo({
      url: `/user/order/order?status=${status}`
    });
  },
  userTap(e) {
    const type = e.currentTarget.dataset.type;
    console.log(type);
    if (type == 'shiming') {
      wx.navigateTo({
        url: `/user/auctionAdd/auctionAdd`
      })
    }else if(type == 'site'){
      wx.navigateTo({
        url: `/user/site/site`
      })
    }else if(type=='bail'){
      wx.navigateTo({
        url: `/user/bail/bail`
      })
    }else if(type=='attention'){
      wx.navigateTo({
        url: `/user/enshrine/enshrine`
      })
    }else if(type=='collect'){
      wx.navigateTo({
        url: `/user/collect/collect`
      })
    }else if(type=='plan'){
      wx.navigateTo({
        url: `/user/plan/plan`
      })
    }else if(type=='auctionBack'){
      wx.navigateTo({
        url: `/user/auctionBack/auctionBack`
      })
    }else if(type=='myAuction'){
      wx.navigateTo({
        url: `/user/myAuction/myAuction`
      })
    }
    
  },
  onShow() {
    this.gain()
    // 订单状态
    this.setData({
      orderList: [{
          title: '待支付',
          iconName: '/img/user/待支付.png',
          orderNum: 0,
          status: 0,
        },
        {
          title: '待发货',
          iconName: '/img/user/待发货.png',
          orderNum: 0,
          status: 1,
        },
        {
          title: '待收货',
          iconName: '/img/user/待收货.png',
          orderNum: 0,
          status: 2,
        },
        {
          title: '完成',
          iconName: '/img/user/完成.png',
          orderNum: 0,
          status: 3,
        },
      ],
    })
  },
  gain() {
    if (wx.getStorageSync('userId')) {
      http.post({
        url: '/User/info',
        data: {
          id: wx.getStorageSync('userId')
        },
        then: (res) => {
          console.log('用户信息', res);
          let data = res.data.data
          this.setData({
            userInfo: data,
            currAuthStep: 3
          })
          wx.hideLoading()
        }
      })
    } else {
      this.setData({
        currAuthStep: 1
      })
      wx.hideLoading()
    }
  },
  // 登录
  wxLogin(e) {
    let detail = e.detail
    if (detail.errMsg = 'getPhoneNumber:ok') {
      console.log('用户同意授权', detail);
      wx.showLoading({
        title: '加载中...'
      });
      wx.login({
        success: (res) => {
          if (res.code) {
            console.log('微信登录');
            this.setData({
              wxCode: res.code
            })
            wx.setStorageSync('wxCode', res.code)
            http.post({
              url: '/User/login',
              header: {
                "content-type": "application/json"
              },
              data: {
                code: res.code
              },
              then: (res) => {
                console.log('微信授权登录', res);
                wx.setStorageSync('userId', res.data.data.id)
                this.gain()
                wx.hideLoading()
              }
            })
          } else {
            console.log('登录失败！' + res.errMsg);
            wx.hideLoading()
          }
        },
      })
    }
  },
  editskip() {
    wx.navigateTo({
      url: `/user/meInfo/meInfo`
    })
  },
});